package com.liutaw.nanjingair.views.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.dagger.HasComponent;
import com.liutaw.nanjingair.dagger.component.ActivityComponent;
import com.liutaw.nanjingair.dagger.component.BaseWeatherComponent;
import com.liutaw.nanjingair.dagger.component.DaggerActivityComponent;
import com.liutaw.nanjingair.dagger.component.DaggerBaseWeatherComponent;
import com.liutaw.nanjingair.dagger.module.ActivityModule;
import com.liutaw.nanjingair.views.adapter.MainPagerAdapter;
import com.liutaw.nanjingair.views.fragments.IndexFragment;
import com.liutaw.nanjingair.views.fragments.OtherCityFragment;
import com.liutaw.nanjingair.views.fragments.ProductsFragment;
import com.liutaw.nanjingair.views.fragments.SettingsFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends FragmentActivity implements HasComponent<BaseWeatherComponent> {
    @Bind(R.id.viewpager)
    ViewPager mViewPager;
    @Bind(R.id.first)
    View firstView;
    @Bind(R.id.second)
    View secondView;
    @Bind(R.id.third)
    View thirdView;
    @Bind(R.id.forth)
    View forthView;
    private BaseWeatherComponent baseWeatherComponent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initDependencyInjection();
        //TODO 判断是否我在南京
        initViews();
        initNanjing();
//        initNotInNanjing();
        clickFirst();
    }

    private void initDependencyInjection() {
        baseWeatherComponent = DaggerBaseWeatherComponent.builder().applicationComponent(((AndroidApplication)getApplication()).getApplicationComponent()).activityModule(new ActivityModule(this)).build();
    }

    private void initViews() {
        initFragments();
    }

    private void initFragments() {
        List<Fragment> fragments = new ArrayList<>();
        Fragment indexFragment = new IndexFragment();
        Fragment otherCityFragment = new OtherCityFragment();
        Fragment productsFragment = new ProductsFragment();
        Fragment settingsFragment = new SettingsFragment();
        fragments.add(indexFragment);
        fragments.add(productsFragment);
        fragments.add(otherCityFragment);
        fragments.add(settingsFragment);
        mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager(),fragments));
        mViewPager.setOffscreenPageLimit(4);
    }

    private void initNanjing() {
        setBottomBarText(firstView, "首页", R.drawable.icon_index_normal);
        setBottomBarText(secondView, "气象产品", R.drawable.icon_product_normal);
        setBottomBarText(thirdView, "其它城市", R.drawable.icon_othercity_normal);
        setBottomBarText(forthView, "设置", R.drawable.icon_settings_normal);
    }

    private void initNotInNanjing() {
    }

    private void setBottomBarText(View view, String text, final int imageResId) {
        TextView textView = (TextView) view.findViewById(R.id.bottombar_item_text);
        textView.setText(text);

        ImageView imageView = (ImageView) view.findViewById(R.id.bottombar_item_image);
        imageView.setImageResource(imageResId);
    }

    @OnClick(R.id.first)
    public void clickFirst() {
        changeBottomView(firstView);
        mViewPager.setCurrentItem(0);
    }

    @OnClick(R.id.second)
    public void clickSecond() {
        changeBottomView(secondView);
        mViewPager.setCurrentItem(1);
    }

    @OnClick(R.id.third)
    public void clickThird() {
        changeBottomView(thirdView);
        mViewPager.setCurrentItem(2);
    }

    @OnClick(R.id.forth)
    public void clickForth() {
        changeBottomView(forthView);
        mViewPager.setCurrentItem(3);
    }

    private View lastView;

    private void changeBottomView(View view) {
        if (view == lastView) {
            return;
        }
        if (lastView == null) {
            lastView = firstView;
        }
        changeBottomBarNotSelected(lastView);
        changeBottomBarSelected(view);
        lastView = view;
    }

    private void changeBottomBarSelected(View view) {
        TextView textView = (TextView) view.findViewById(R.id.bottombar_item_text);
        textView.setTextColor(getResources().getColor(R.color.text_color_blue));
        ImageView imageView = (ImageView) view.findViewById(R.id.bottombar_item_image);
        switch (view.getId()) {
            case R.id.first:
                imageView.setImageResource(R.drawable.icon_index_pressed);
                break;
            case R.id.second:
                imageView.setImageResource(R.drawable.icon_product_pressed);
                break;
            case R.id.third:
                imageView.setImageResource(R.drawable.icon_othercity_pressed);
                break;
            case R.id.forth:
                imageView.setImageResource(R.drawable.icon_settings_pressed);
                break;
        }
    }

    private void changeBottomBarNotSelected(View view) {
        TextView textView = (TextView) view.findViewById(R.id.bottombar_item_text);
        textView.setTextColor(getResources().getColor(R.color.text_gray_color));
        ImageView imageView = (ImageView) view.findViewById(R.id.bottombar_item_image);
        switch (view.getId()) {
            case R.id.first:
                imageView.setImageResource(R.drawable.icon_index_normal);
                break;
            case R.id.second:
                imageView.setImageResource(R.drawable.icon_product_normal);
                break;
            case R.id.third:
                imageView.setImageResource(R.drawable.icon_othercity_normal);
                break;
            case R.id.forth:
                imageView.setImageResource(R.drawable.icon_settings_normal);
                break;
        }
    }

    @Override
    public BaseWeatherComponent getComponent() {
        return baseWeatherComponent;
    }
}
